package tms.view;

import java.util.Date;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.UIManager;

import tms.Helper;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;

public class SummaryPanel extends JPanel {

    private static final long serialVersionUID = 1L;

    private JTextArea txtDepartureInfo;
    private JTextArea txtReturnInfo;
    private JLabel lblGrandTotal;
    private JLabel lblClass;
    public SummaryPanel() {
        setLayout(new FormLayout(new ColumnSpec[] {
                FormSpecs.UNRELATED_GAP_COLSPEC,
                FormSpecs.UNRELATED_GAP_COLSPEC,
                FormSpecs.RELATED_GAP_COLSPEC,
                ColumnSpec.decode("default:grow"),
                FormSpecs.RELATED_GAP_COLSPEC,
                FormSpecs.DEFAULT_COLSPEC,
                FormSpecs.RELATED_GAP_COLSPEC,
                ColumnSpec.decode("default:grow"),
                FormSpecs.RELATED_GAP_COLSPEC,
                ColumnSpec.decode("default:grow"),
                FormSpecs.UNRELATED_GAP_COLSPEC},
            new RowSpec[] {
                FormSpecs.RELATED_GAP_ROWSPEC,
                FormSpecs.DEFAULT_ROWSPEC,
                FormSpecs.RELATED_GAP_ROWSPEC,
                RowSpec.decode("top:max(6dlu;default)"),
                FormSpecs.RELATED_GAP_ROWSPEC,
                FormSpecs.DEFAULT_ROWSPEC,
                FormSpecs.RELATED_GAP_ROWSPEC,
                FormSpecs.DEFAULT_ROWSPEC,
                FormSpecs.UNRELATED_GAP_ROWSPEC,
                FormSpecs.DEFAULT_ROWSPEC,
                FormSpecs.UNRELATED_GAP_ROWSPEC,
                FormSpecs.DEFAULT_ROWSPEC,
                FormSpecs.UNRELATED_GAP_ROWSPEC}));

        JLabel lblTitle = Tools.createLabel("Booking Summary", Tools.TAHOMA_BOLD_16);
        add(lblTitle, "2, 2, 5, 1, left, default");

        add(new JSeparator(), "2, 4, 9, 1, default, top");

        JLabel lblDeparture = Tools.createLabel("Departure Flight", Tools.TAHOMA_BOLD_12);
        add(lblDeparture, "3, 6, 4, 1, left, default");

        JLabel lblReturn = Tools.createLabel(
                "Return Flight (shown if round trip ticket) ", Tools.TAHOMA_BOLD_12);
        add(lblReturn, "7, 6, 2, 1, left, default");

        txtDepartureInfo = new JTextArea("From & To\r\nDeparture Date\r\nFare");
        txtDepartureInfo.setFocusable(false);
        txtDepartureInfo.setEditable(false);
        txtDepartureInfo.setBackground(UIManager.getColor("Button.background"));
        txtDepartureInfo.setFont(Tools.TAHOMA_PLAIN_12);
        add(txtDepartureInfo, "4, 8, fill, top");

        txtReturnInfo = new JTextArea("From & To\r\nDeparture Date\r\nFare");
        txtReturnInfo.setFocusable(false);
        txtReturnInfo.setEditable(false);
        txtReturnInfo.setBackground(UIManager.getColor("Button.background"));
        txtReturnInfo.setFont(Tools.TAHOMA_PLAIN_12);
        add(txtReturnInfo, "8, 8, fill, top");

        lblClass = Tools.createLabel("Carbin/Class:", Tools.TAHOMA_BOLD_12);
        add(lblClass, "3, 10, 2, 1");

        lblGrandTotal = Tools.createLabel("Grand total:", Tools.TAHOMA_BOLD_12);
        add(lblGrandTotal, "3, 12, 2, 1");
    }

    public void setDepartureInfo(String from, String to, Date date, Double fare) {
        txtDepartureInfo.setText(buildFlightInfo(from, to, date, fare));
    }

    public void setReturnInfo(String from, String to, Date date, Double fare) {
        txtReturnInfo.setText(buildFlightInfo(from, to, date, fare));
    }

    public void setTicketClass(String ticketClass) {
        lblClass.setText("Carbin/Class: " + ticketClass);
    }

    public void setGrandTotal(double grandTotal) {
        lblGrandTotal.setText("Grand Total: " + grandTotal + "$");
    }

    /**
     * Build a string from flight information.
     * @param from
     * @param to
     * @param date
     * @param fare
     * @return
     */
    private String buildFlightInfo(String from, String to, Date date, Double fare) {
        StringBuilder builder = new StringBuilder();
        builder.append("From: ").append(from);
        builder.append("\t");
        builder.append("To: ").append(to);
        builder.append("\n");
        builder.append("Depature date: ").append(
                Helper.dateToString(date, Tools.DATE_FORMAT_NO_TIME_2));
        builder.append("\n");
        builder.append("Fare: ").append(fare).append("$");
        return builder.toString();
    }
}
